package java8;

import java.util.Objects;
import java.util.function.BiPredicate;
import java.util.function.DoublePredicate;
import java.util.function.IntPredicate;
import java.util.function.LongPredicate;
import java.util.function.Predicate;

public class PredicateSample {

    public static void main(String[] args) {
        // Predicate: 有入参, 须返回布尔值
        Predicate<String> isNullPredicate = Objects::isNull;
        // 输出: false
        System.out.println(isNullPredicate.test(""));

        // negate: 非逻辑
        Predicate<String> isNotNullPredicate = isNullPredicate.negate();
        // 输出: true
        System.out.println(isNotNullPredicate.test(""));

        // and: 与逻辑
        Predicate<String> isNotEmptyPredicate = str -> str.length() > 0;
        Predicate<String> isNotNullAndNotEmptyPredicate = isNotNullPredicate.and(isNotEmptyPredicate);
        // 输出: false
        System.out.println(isNotNullAndNotEmptyPredicate.test(""));

        // or: 或逻辑
        Predicate<String> isEmptyPredicate = str -> str.length() == 0;
        Predicate<String> isNullOrEmptyPredicate = isNullPredicate.or(isEmptyPredicate);
        // 输出: true
        System.out.println(isNullOrEmptyPredicate.test(""));

        // ================================================
        // IntPredicate
        // LongPredicate
        // DoublePredicate
        // BiPredicate
        // ================================================
        // IntPredicate: int 类型入参, 返回布尔值结果
        IntPredicate intPredicate = i -> i == 0;
        // LongPredicate: Long 类型入参, 返回布尔值结果
        LongPredicate longPredicate = l -> l == 0;
        // DoublePredicate: Double 类型入参, 返回布尔值结果
        DoublePredicate doublePredicate = d -> d == 0;
        // BiPredicate: 二元任意类型的入参, 返回布尔值结果
        BiPredicate<Integer, Integer> biPredicate = Objects::equals;
    }

}